package com.example.basic.web.service.device.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.PinyinUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.utils.WubiUtils;
import com.example.basic.web.enums.DataCodeEnum;
import com.example.basic.web.enums.DeviceStatusEnum;
import com.example.basic.web.enums.EnableTypeEnum;
import com.example.basic.web.mapper.device.DeviceMapper;
import com.example.basic.web.model.device.Device;
import com.example.basic.web.service.dataCode.DataCodeService;
import com.example.basic.web.service.device.DeviceService;
import com.example.basic.web.service.transTask.TransTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class DeviceServiceImpl extends BaseEntityServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private DataCodeService dataCodeService;

    @Resource
    private TransTaskService transTaskService;


    /**
     * 保存或者修改
     * @param device 模型
     * @return
     */
    @Override
    public Device addOrUpdate(Device device) {
        //参数检验
        notNull(device.getDeviceName(),"应用终端名称不能为空");

        //校验名称是否重复
        LambdaQueryWrapper<Device> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Device::getDeviceName,device.getDeviceName());
        if(StringUtils.notEmptyParams(device.getId())){
            lqw.ne(Device::getId,device.getId());
        }
        Device localDevice = this.mapper.selectOne(lqw);
        if(localDevice != null){
            throw new ServiceException("设备名称已维护");
        }
        //修改
        if (device.getId() != null) {
            preUpdate(device);
        }
        //新增
        else {
            //获取最大序号
            Integer sortNo = deviceMapper.getMaxSortNo();
            device.setSortNo(sortNo);
            //生成应用终端编码
            String deviceCode = dataCodeService.generateDataCodeNo(DataCodeEnum.DEVICE_CODE.getType());
            device.setDeviceCode(deviceCode);
            device.setStatus(DeviceStatusEnum.standby.getType());
            preCreate(device);
        }

        //生成拼音码五笔码
        generatePyWbCode(device);

        this.save(device);
        return detail(device.getDevicePoint());
    }

    /**
     * @create 生成五笔码、拼音码
     * @param device 系统用户模型实体类
     */
    private void generatePyWbCode(Device device) {
        // 获取应用终端名
        String deviceName = StringUtils.safeToString(device.getDeviceName(), "");
        // 生成拼音码
        if("".equals(StringUtils.safeToString(device.getPyCode(),"").replaceAll(" ",""))) {
            String userNamePinyin = PinyinUtils.getSimplePinYin(deviceName);
            device.setPyCode(userNamePinyin);
        }
        // 生成拼音码
        if("".equals(StringUtils.safeToString(device.getWbCode(),"").replaceAll(" ",""))) {
            String userNameWubi = WubiUtils.getSimpleWuBi(deviceName);
            device.setWbCode(userNameWubi);
        }
    }

    /**
     * 详情
     * @param devicePoint 应用终端编码
     * @return
     */
    @Override
    public Device detail(String devicePoint) {
        notNull(devicePoint, "应用终端编码不能为空");
        return deviceMapper.detail(devicePoint);
    }

    /**
     * 删除
     * @param devicePoint 应用终端编码
     * @return
     */
    @Override
    public int delete(String devicePoint) {
        notNull(devicePoint,"应用终端编码不能为空");

        Device device = detail(devicePoint);
        if(device == null){
            throw new ServiceException("未查询到当前数据");
        }
        return this.mapper.deleteById(device.getId());
    }

    /**
     * 启用/禁用
     * @param devicePoint 应用终端编码
     * @return
     */
    @Override
    public int enableOrDisable(String devicePoint) {
        notNull(devicePoint,"应用终端编码不能为空");

        Device device = detail(devicePoint);

        if(device == null){
            throw new ServiceException("未查询到当前数据");
        }

        //停用启用
        if(device.getEnableFlag().equals(EnableTypeEnum.ON.getType())){
            device.setEnableFlag(EnableTypeEnum.OFF.getType());
        }else {
            device.setEnableFlag(EnableTypeEnum.ON.getType());
        }

        preUpdate(device);

        return this.mapper.updateById(device);
    }

    /**
     * 应用终端列表
     * @param queryPage 分页
     * @param deviceType 设备类型 0-全部
     * @param isAll 是否查询全部 0-否，1-是
     * @param status 状态 0-全部
     * @return
     */
    @Override
    public IPage<Device> queryList(Page<Device> queryPage, String deviceType, String isAll,String status) {
        return deviceMapper.queryList(queryPage,deviceType,isAll,status);
    }

    /**
     * 空闲/占用  zzl
     * @param deviceCode 应用终端编码
     * @return
     */
    @Override
    public int freeOrUse(String deviceCode) {
        notNull(deviceCode,"应用终端编码不能为空");

        Device device = detail(deviceCode);

        if(device == null){
            throw new ServiceException("设备不存在");
        }

        //停用启用
        if(device.getStatus().equals(DeviceStatusEnum.standby.getType())){
            device.setStatus(DeviceStatusEnum.work.getType());
        }else {
            device.setStatus(DeviceStatusEnum.standby.getType());
        }

        preUpdate(device);

        return this.mapper.updateById(device);
    }

    /**
     * 首页设备情况
     * @return
     */
    @Override
    public List<Map> homeDeviceReport() {
        return this.mapper.homeDeviceReport();
    }

    /**
     * 查询未使用的接驳台
     * @param deviceAddress
     * @return
     */
    @Override
    public Device findUnUseDockStation(String deviceAddress,String deviceType) {
        notNull(deviceAddress,"设备地址不能为空");
        return this.mapper.findUnUseDockStation(deviceAddress,deviceType);
    }

    /**
     * 根据设备点位获取设备信息
     * @param devicePoint
     * @return
     */
    @Override
    public Device findByPoint(String devicePoint) {

        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getDevicePoint,devicePoint);
        return this.mapper.selectOne(queryWrapper);
    }

    /**
     * 根据设备名称获取设备信息
     * @param deviceName
     * @return
     */
    @Override
    public Device findByDeviceName(String deviceName) {
        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getDeviceName,deviceName);
        return this.mapper.selectOne(queryWrapper);
    }

    /**
     * 获取工作中的接驳台
     * @param deviceAddress
     * @return
     */
    @Override
    public Device findWorkDevice(String deviceAddress) {
        return this.mapper.findWorkDevice(deviceAddress);
    }

    @Override
    public List<Device> queryListByType(String deviceType) {
        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getDeviceType,deviceType);
        queryWrapper.eq(Device::getStatus,DeviceStatusEnum.standby.getType());
        queryWrapper.eq(Device::getEnableFlag,EnableTypeEnum.ON.getType());
        return this.mapper.selectList(queryWrapper);

    }
}
